package com.azusa.ktc_spit.controller;

import com.azusa.ktc_spit.entity.Spit;
import com.azusa.ktc_spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description 职位 控制器层
 * @author Azusa
 * @date 2020-10-10 18:33:43
*/
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    /**
    * 查询全部数据
    * @return
    */
    @RequestMapping(method= RequestMethod.GET)
    public Result findAll(){
        return new Result(StatusCode.SUCCESS,spitService.findAll());
    }

    /**
    * 根据ID查询
    * @param id ID
    * @return
    */
    @RequestMapping(value="/{id}",method= RequestMethod.GET)
    public Result findById(@PathVariable String id){
        return new Result(StatusCode.SUCCESS,spitService.findById(id));
    }

    /**
    * 分页+多条件查询
    * @param spit 查询条件封装
    * @param page 页码
    * @param size 页大小
    * @return 分页结果
    */
    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
        public Result findSearch(@RequestBody Spit spit , @PathVariable int page, @PathVariable int size){
        Page<Spit> searchPage = spitService.findSearchPage(page, size, spit);
        return  new Result(StatusCode.SUCCESS, new PageResult<Spit>(searchPage.getTotalElements(), searchPage.getContent() ));
    }

    /**
    * 根据条件查询
    * @param spit
    * @return
    */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Spit spit){
        return new Result(StatusCode.SUCCESS, spitService.findSearch(spit));
    }

    /**
    * 增加
    * @param spit
    */
    @RequestMapping(method=RequestMethod.POST)
    public Result add(@RequestBody Spit spit){
        spitService.add(spit);
        return new Result(StatusCode.SUCCESS);
    }

    /**
    * 修改
    * @param spit
    */
    @RequestMapping(value="/{id}",method= RequestMethod.PUT)
    public Result update(@RequestBody  Spit  spit, @PathVariable String id ){
        spit.setId(id);
        spitService.update( spit);
        return new Result(StatusCode.SUCCESS);
    }

    /**
    * 删除
    * @param id
    */
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        spitService.deleteById(id);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 点赞吐槽
     * @param spitId
     * @return
     */
    @PutMapping("thumbup/{spitId}")
    public Result thumbUpSpit(@PathVariable("spitId") String spitId){
        //userId先写死，后期从缓存数据库中获取
        spitService.thumbUpSpit("1002",spitId);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 根据父id查询吐槽列表分页
     * @param pageNum
     * @param pageSize
     * @param parentId
     * @return
     */
    @GetMapping("comment/{parentId}/{page}/{size}")
    public Result findComment(@PathVariable("page")int pageNum,@PathVariable("size")int pageSize,@PathVariable("parentId") String parentId){
        Page<Spit> page = spitService.findComment(pageNum, pageSize, parentId);
        return new Result(StatusCode.SUCCESS,new PageResult<Spit>(page.getTotalElements(),page.getContent()));
    }
}
